$(function () {
	 $.get("../disappearalarmdetal/disappearalarm/list", function(r){
			vm.disappearalarm = r.listDisappearalarm;
	        var list = r.listDisappearalarm;
	        var html='';
	        html='<li class="list-group-item">离岗策略列表</li>';
	        $.each(list,function(index,item){
	        	html+='<li class="list-group-item"  onclick="show_sub('+item.id+')">'+item.name+'</li>';
	        });
	        $("#disappearalarmdetal").html(html);
	        
	    });
		$.get("../disappearalarmdetal/station/list", function(r){
			vm.station = r.listStation;
	       
	        
	    });
		$.get("../disappearalarmdetal/area/list", function(r){
			vm.area = r.listArea;
	        var list = r.listArea;
	        var html='';
	        html+='<option value="0">请选择</option>';
	        $.each(list,function(index,item){
	        	
	        	html+='<option value="'+item.id+'">'+item.name+'</option>';
	        	
	        });
	        $("#area").html(html);
	        
	    });
	
    $("#jqGrid").jqGrid({
        url: '../disappearalarmdetal/list',
        datatype: "json",
        colModel: [			
			{ label: '离岗策略', name: 'idDisappearalarm', width: 80,formatter:customFmatter }, 			
			{ label: '基站', name: 'idStation', width: 80 ,formatter:customFmatter1}, 			
			{ label: '时延', name: 'delay', width: 80 }			
        ],
		viewrecords: true,
		   height: 'auto',
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    
   
    
    
});

function customFmatter1(cellvalue, options, rowObject){ 
	
	var name;
	
	  $.each(vm.station,function(index,item){
      	
		  if(cellvalue==item.id){
				
				name=item.name;
			}
      	
      });
	  //alert(name);

    return name;  
}
function customFmatter(cellvalue, options, rowObject){ 
	
	var name;
	
	$.each(vm.disappearalarm,function(index,item){
		
		if(cellvalue==item.id){
			
			name=item.name;
		}
		
	});
	
	
	return name;  
}

function show_sub(v){   
	
	vm.showList = true;
	vm.idDisappearalarm=v;
	var page = $("#jqGrid").jqGrid('getGridParam','page');
	$("#jqGrid").jqGrid('setGridParam',{
		  postData:{'idDisappearalarm': v},
        page:page
    }).trigger("reloadGrid");
}

function show_sub_station(idArea){//基站列表

	$.get("../disappearalarmdetal/station/list/"+idArea+"/"+vm.idDisappearalarm, function(r){
		vm.station = r.listStation;
        var list = r.listStation;
        var html='';
        $.each(list,function(index,item){
       	html+=' <div class="col-md-3">'
        	   +' <div class="input-group">'
        	   +' <span class="input-group-addon">'
        	   +'<input type="checkbox" name="station_checkbox" id="'+item.id+'">'+item.name+'||'+item.code+''
        	   +'</span>'
        	   +' <input type="text" name="delay" class="form-control"  placeholder="时延（秒）" value="5">'
        	   +' </div>'
        	   +' </div>';
        	
        });
        $("#station").html(html);
        
    });
}

function checkAll(XelementName){//全选
	 var checkboxes=document.getElementsByName(XelementName);
	    for(var i=0;i<checkboxes.length;i++)
	    {
	            checkboxes[i].checked=true;
	    }
}
function checkNotAll(XelementName){//反选
	 var checkboxes=document.getElementsByName(XelementName);
	    for(var i=0;i<checkboxes.length;i++)
	    {
	            checkboxes[i].checked=false;
	    }
}
function setDelay(){//设计延迟
	alert($("#delay").val());
	$("input[name='delay']:text").each(function(index,item){ 
	
		$(this).val($("#delay").val());//填充内容
	});
}

function addSome(){//添加
	
	
	var list=new Array();
	$("input[name='station_checkbox']:checkbox:checked").each(function(index,item){ 
		var idStation=$(this).attr("id");
		var entity=new Object();
		entity.idStation=idStation;
		entity.idDisappearalarm=vm.idDisappearalarm;
		 entity.delay= $(this).parent().next().val();
		 list.push(entity);
	}) ;
	console.log(JSON.stringify(list));
	var url="../disappearalarmdetal/addBatch";
	$.ajax({
		type: "POST",
	    url: url,
	    dataType:"json",
	    contentType:"application/json",
	    data: JSON.stringify(list),
	    success: function(r){
	    	if(r.code === 0){
				alert('操作成功', function(index){
					history.go(0);
				});
			}else{
				alert(r.msg);
			}
		}
	});
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		disappearalarmdetal: {},
		station:{},
		area:{},
		idArea:{},
        idDisappearalarm:{}
	},
	
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			if(JSON.stringify(vm.idDisappearalarm)=="{}"){
				alert("请先选择离岗策略组");
				return ;
			}
			
			vm.showList = false;
			vm.title = "新增";
			
			vm.disappearalarmdetal = {idDisappearalarm:vm.idDisappearalarm};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.disappearalarmdetal.id == null ? "../disappearalarmdetal/save" : "../disappearalarmdetal/update";
			$.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.disappearalarmdetal),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}


			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../disappearalarmdetal/delete",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../disappearalarmdetal/info/"+id, function(r){
                vm.disappearalarmdetal = r.disappearalarmdetal;
            });
		},
		addBatch:function(){
			if(JSON.stringify(vm.idDisappearalarm)=="{}"){
				alert("请先选择越界策略组");
				return ;
			}
			$('#form1').hide();
			$('#form2').hide();
			$('#form3').show();
			
		},
		
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});